package com.github.caijh.mall.system.controller;

import javax.inject.Inject;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.caijh.commons.util.Strings;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.common.PageReqBody;
import com.github.caijh.mall.system.model.SystemAttachment;
import com.github.caijh.mall.system.request.SystemAttachmentMoveRequest;
import com.github.caijh.mall.system.request.SystemAttachmentRequest;
import com.github.caijh.mall.system.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/admin/system/attachment")
@Api(tags = "附件管理") //配合swagger使用
public class SystemAttachmentController extends BaseController {

    @Inject
    private SystemAttachmentService systemAttachmentService;

    /**
     * 分页显示附件管理表
     *
     * @param pid         pid
     * @param pageReqBody 请求体
     * @return SystemAttachment Page
     */
    @ApiOperation(value = "分页列表") //配合swagger使用
    @PostMapping(value = "/list")
    public IPage<SystemAttachment> page(
            @RequestParam Integer pid,
            @Validated PageReqBody pageReqBody) {
        return systemAttachmentService.page(pid, pageReqBody);
    }

    /**
     * 新增附件管理表
     *
     * @param systemAttachmentRequest 新增参数
     * @author Mr.Zhang
     * @since 2020-06-02
     */
    @ApiOperation(value = "新增")
    @PostMapping(value = "/add")
    public void save(@RequestBody @Validated SystemAttachmentRequest systemAttachmentRequest) {
        SystemAttachment systemAttachment = new SystemAttachment();
        BeanUtils.copyProperties(systemAttachmentRequest, systemAttachment);
        systemAttachmentService.save(systemAttachment);
    }

    /**
     * 删除附件管理表
     *
     * @param ids String
     * @author Mr.Zhang
     * @since 2020-06-02
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    public CommonResult<String> delete(@PathVariable String ids) {
        if (systemAttachmentService.removeByIds(Strings.toIntListByComma(ids))) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改附件管理表
     *
     * @param id                      integer id
     * @param systemAttachmentRequest 修改参数
     * @author Mr.Zhang
     * @since 2020-06-02
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id,
                                       @RequestBody @Validated SystemAttachmentRequest systemAttachmentRequest) {
        SystemAttachment systemAttachment = new SystemAttachment();
        BeanUtils.copyProperties(systemAttachmentRequest, systemAttachment);
        systemAttachment.setAttId(id);

        if (systemAttachmentService.updateById(systemAttachment)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询附件管理表信息
     *
     * @param move SystemAttachmentMoveRequest
     * @author Mr.Zhang
     * @since 2020-06-02
     */
    @ApiOperation(value = "更改图片目录")
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    public CommonResult<String> updateAttrId(@RequestBody @Validated SystemAttachmentMoveRequest move) {
        LambdaUpdateWrapper<SystemAttachment> lup = new LambdaUpdateWrapper<>();
        lup.in(SystemAttachment::getAttId, Strings.toIntListByComma(move.getAttrId()));
        lup.set(SystemAttachment::getPid, move.getPid());
        if (systemAttachmentService.update(lup)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询附件管理表信息
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-06-02
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public CommonResult<SystemAttachment> info(@PathVariable Integer id) {
        SystemAttachment systemAttachment = systemAttachmentService.getById(id);
        return CommonResult.success(systemAttachment);
    }

}



